//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    console.log('getUserInfo 函数开始执行');
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              console.log('用户数据获取成功');
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  getCrtLoc:function(cb){
    wx.getLocation({
  type: 'gcj02',
  success: function(res) {
      console.log('位置获取成功')
      console.log(res)
      var lat = res.latitude;
      var lng = res.longitude;
      var url = 'http://api.map.baidu.com/geocoder/v2/?location='+lat+','+lng+'&output=json&pois=1&radius=1&ak=xvwfrE9SY97e2oxhCb6wECCSH0PsEpng';
      wx.request({
        url: url,
        success: function(res) {
          console.log('逆地理编码执行成功')
          var city = res.data.result.addressComponent.city
          var cityInfo = {"city":city,"lat":lat,"lng":lng}
          wx.setStorageSync('cityInfo', cityInfo)
          typeof cb == "function" && cb(cityInfo.city)
        },
        fail:function(res){
          console.log('逆地理编码执行失败')
          console.log(res)
        }
      })
    }
})
  },
  //数据加载方法 yjmp
  sendReq:function(cb){
    //显示加载动画
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    //向服务器发送request请求
    //数据写回请求页面
    //停止加载动画
    setTimeout(function(){
      wx.hideToast()
    },2000)
  },
  globalData:{
    userInfo:null
  }
})